package com.txsj.scan.activity.allocationapply;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.adapter.AllocationAddGoodsListAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.AllocationApplyDetailsBean;
import com.txsj.scan.bean.EventAllocationBean;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.presenter.AllocationCheckStatusPresenter;
import com.txsj.scan.utils.GsonFactory;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.IAllocationCheckStatusView;
import com.txsj.scan.widget.TSnackbarView;
import com.txsj.scan.widget.TitleBarView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AllocationCheckStatusActivity extends BaseActivity<AllocationCheckStatusPresenter> implements IAllocationCheckStatusView {
    @Override
    protected AllocationCheckStatusPresenter providePresenter() {
        return new AllocationCheckStatusPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    TitleBarView titleBar;
    TextView tv_form_num, tv_warehouse_out, tv_warehouse_in;
    RecyclerView recyclerView;
    TextView tv_goods_categoryCount, tv_goods_count, tv_remark;
    AllocationApplyDetailsBean detailsBean;
    AllocationAddGoodsListAdapter addGoodsListAdapter;
    List<AllocationApplyDetailsBean.DetailsBean> showGoodsList;
    boolean isEpc;
    int outWareHouseId;
    String outWareHouseName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_allocation_add);
        titleBar = (TitleBarView) findViewById(R.id.titleBar);
        tv_form_num = (TextView) findViewById(R.id.tv_form_num);
        tv_warehouse_out = (TextView) findViewById(R.id.tv_warehouse_out);
        tv_warehouse_in = (TextView) findViewById(R.id.tv_warehouse_in);
        tv_goods_categoryCount = (TextView) findViewById(R.id.tv_goods_categoryCount);
        tv_goods_count = (TextView) findViewById(R.id.tv_goods_count);
        tv_remark = (TextView) findViewById(R.id.tv_remark);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        addGoodsListAdapter = new AllocationAddGoodsListAdapter(this);
        recyclerView.setAdapter(addGoodsListAdapter);
        presenter.getWarehouseHttp();
        Intent intent = getIntent();
        isEpc = intent.getBooleanExtra("isEpc", false);
        detailsBean = GsonFactory.parseJsonWithGson(intent.getStringExtra("detailsBean"), AllocationApplyDetailsBean.class);
        if (detailsBean != null) {
            tv_warehouse_in.setText(detailsBean.getInto_warehouse_name());
            tv_warehouse_out.setText(detailsBean.getOut_warehouse_name());
            tv_goods_categoryCount.setText(detailsBean.getGoods_category_count() + "");
            tv_goods_count.setText(detailsBean.getGoods_count() + "");
            tv_form_num.setText(detailsBean.getBill_number());
            if (!TextUtils.isEmpty(detailsBean.getBill_remark())) {
                tv_remark.setText(detailsBean.getBill_remark());
            } else {
                tv_remark.setText("暂无");
            }

            if (isEpc) {
                String goodsData = intent.getStringExtra("data");
                if (!TextUtils.isEmpty(goodsData)) {
                    List<GoodsInfo> goodsList = GsonFactory.getObjectList(goodsData, GoodsInfo.class);
                    showGoodsList = new ArrayList<>();
                    for (int i = 0; i < goodsList.size(); i++) {
                        AllocationApplyDetailsBean.DetailsBean mGoods = new AllocationApplyDetailsBean.DetailsBean();
                        mGoods.setGoods_id(goodsList.get(i).getGoods_id());
                        mGoods.setGoods_name(goodsList.get(i).getGoods_name());
                        mGoods.setGoods_count(goodsList.get(i).getEpcList().size());
                        mGoods.setGoods_img(goodsList.get(i).getGoods_img());
                        mGoods.setEpcList(goodsList.get(i).getEpcList());
                        showGoodsList.add(mGoods);
                    }
                    addGoodsListAdapter.setData(showGoodsList);
                }
            } else {
                showGoodsList = detailsBean.getDetails();
                addGoodsListAdapter.setData(detailsBean.getDetails());
            }
        }

        titleBar.setRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoading();
                if (isEpc) {
                    StringBuffer buffer = new StringBuffer();
                    for (int i = 0; i < showGoodsList.size(); i++) {
                        for (int j = 0; j < showGoodsList.get(i).getEpcList().size(); j++) {
                            buffer.append(showGoodsList.get(i).getEpcList().get(j).getStrEpc());
                            buffer.append(",");
                        }
                    }
                    String epcStr = buffer.toString().substring(0, buffer.toString().length() - 1);
                    presenter.allocationCehckStatus(outWareHouseId + "", outWareHouseName, detailsBean.getBill_id() + "",
                            detailsBean.getBill_remark(), "", epcStr);
                } else {
                    ArrayList<HashMap<String, String>> commitBatchList = new ArrayList<>();
                    for (int i = 0; i < detailsBean.getDetails().size(); i++) {
                        for (int j = 0; j < detailsBean.getDetails().get(i).getBatchList().size(); j++) {
                            if (detailsBean.getDetails().get(i).getBatchList().get(j).getChange_num() > 0) {
                                HashMap<String, String> map = new HashMap<>();
                                map.put("batch_id", detailsBean.getDetails().get(i).getBatchList().get(j).getBatch_id() + "");
                                map.put("gs1_num", detailsBean.getDetails().get(i).getBatchList().get(j).getGs1_num() + "");
                                map.put("goods_count", detailsBean.getDetails().get(i).getBatchList().get(j).getChange_num() + "");
                                commitBatchList.add(map);
                            }
                        }
                    }
                    presenter.allocationCehckStatus(outWareHouseId + "", outWareHouseName, detailsBean.getBill_id() + "",
                            detailsBean.getBill_remark(), new Gson().toJson(commitBatchList), "");
                }
            }
        });
    }

    @Override
    public void showLoading() {
        super.showLoading();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
    }

    @Override
    public void loadWareHouseList(List<WarehouseBean> wareHouseList) {
        if (wareHouseList != null && wareHouseList.size() > 0) {
            for (int i = 0; i < wareHouseList.size(); i++) {
                if (wareHouseList.get(i).getWarehouse_type() == 0) {
                    outWareHouseId = wareHouseList.get(i).getWarehouse_id();
                    outWareHouseName = wareHouseList.get(i).getWarehouse_name();
                    tv_warehouse_out.setText(outWareHouseName);
                    break;
                }
            }
        }
    }


    @Override
    public void checkSuccess() {
        hideLoading();
        t("审核成功");
        EventBus.getDefault().post(new EventAllocationBean(true));
        Intent intent = new Intent(this, AllocationApplyActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void checkFailed(String errMsg) {
        new TSnackbarView(tv_form_num, errMsg);
        hideLoading();
    }
}
